import pymysql


class UseMysql():

    def __init__(self,db_config):
        self.db = pymysql.connect(**db_config)
        self.cur = self.db.cursor()

    def insert_data(self,time,newBug,solveBug,unSolveBug):
        '''定义插入语句'''
        select_sql = "select * from bug_report where time='%s';" % time
        insert_sql = "insert into bug_report values ('%s','%s','%s','%s')" %(time,newBug,solveBug,unSolveBug)
        del_sql = "delete from bug_report where time='%s'" % time
        try :
            if self.select_data(select_sql)!=[]:
                self.cur.execute(del_sql)
                self.db.commit()
            self.cur.execute(insert_sql)
            self.db.commit()
        except Exception as ex:
            print(ex)
            self.db.rollback()
    def select_data(self,sql):
        '''定义查询语句'''
        res = []
        try:
            self.cur.execute(sql)
            result = self.cur.fetchall()
            for i in result:
                res.append(i)
        except:
            print('错误的查询')
        return res
    def close_db(self):
        self.db.close()



if __name__=='__main__':
    db_config = {'host': '192.168.1.161',
                     'port': 3306,
                     'user': 'root',
                     'password': 'root',
                     'database': 'report',
                     'charset': 'utf8'}
    my = UseMysql(db_config)
    sql = "select * from newbug where time='2019-12-12'"
    if str(my.select_data(sql)[0][0])=='2019-12-12':
        print('dsa')
    my.close_db()
